package net.youpoint.core.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import com.google.appengine.api.datastore.Key;

@ManagedBean(name = "questionBean")
@RequestScoped
public class Question implements Serializable {

	private static final long serialVersionUID = 8876438104196580302L;

	public static class Answer implements Entry<String, Integer>, Serializable {

		private static final long serialVersionUID = -3104717887539746788L;

		private Key entityKey;
		private String txt;
		private int pop;

		public Answer() {
			super();
			this.txt = "empty";
			this.pop = 0;
		}

		public Answer(final String txt, final int pop) {
			super();
			this.txt = txt;
			this.pop = pop;
		}

		public String getTxt() {
			return this.txt;
		}

		public void setTxt(final String txt) {
			this.txt = txt;
		}

		public int getPop() {
			return this.pop;
		}

		public void setPop(final int pop) {
			this.pop = pop;
		}

		@Override
		public String getKey() {
			return this.txt;
		}

		@Override
		public Integer getValue() {
			return this.pop;
		}

		@Override
		public Integer setValue(final Integer value) {
			final Integer old = this.pop;

			this.pop = value;

			return old;
		}

		public Key getEntityKey() {
			return this.entityKey;
		}

		public void setEntityKey(final Key entityKey) {
			this.entityKey = entityKey;
		}
	}

	private Key entityKey;
	private String txt;
	private Date dtCreate;
	private Date dtDelete;
	private boolean voted;
	private List<Answer> answers = new ArrayList<Answer>();

	public Question() {
		super();

		this.entityKey = null;
		this.txt = "";
		this.dtCreate = new Date();
		this.dtDelete = null;
		this.voted = false;

		this.answers = new ArrayList<Answer>();
	}

	/**
	 * get url like
	 * http://chart.apis.google.com/chart?cht=p3&chs=250x100&chd=t:60
	 * ,40&chl=Hello|World of question diagram to build it by google chart api *
	 * 
	 * @return url to retreive diagram
	 */
	public String getChartUrl() {
		final GChartData cd = new GChartData();

		final List<Entry<String, Integer>> itms = new ArrayList<Entry<String, Integer>>();
		itms.addAll(this.answers);
		cd.setItems(itms);

		cd.setTitle(getTxt());

		return cd.getChartUrl();
	}

	/**
	 * add question action
	 * 
	 * @return redirect to new question form
	 */
	public void edit() {
		System.out.println("edit");
		// return PageConstants.EDIT_QUESTION;
	}

	public void setTxt(final String txt) {
		this.txt = txt;
	}

	public String getTxt() {
		return this.txt;
	}

	public int getPop() {
		int res = 0;
		for (final Answer ans : this.answers) {
			res += ans.getPop();
		}
		return res;
	}

	public Date getDtCreate() {
		return this.dtCreate;
	}

	public List<Answer> getAnswers() {
		return this.answers;
	}

	public void setAnswers(final List<Answer> answers) {
		this.answers = answers;
	}

	public Key getEntityKey() {
		return this.entityKey;
	}

	public void setEntityKey(final Key entityKey) {
		this.entityKey = entityKey;
	}

	public Date getDtDelete() {
		return this.dtDelete;
	}

	public void setDtDelete(final Date dtDelete) {
		this.dtDelete = dtDelete;
	}

	public void setDtCreate(final Date dtCreate) {
		this.dtCreate = dtCreate;
	}

	public boolean isVoted() {
		return this.voted;
	}

	public void setVoted(final boolean voted) {
		this.voted = voted;
	}
}